// ==UserScript==
// @name Google doc Navigation panel
// @author Jiahua Huang
// @version 1.2.1
// @description Navigation panel for Google docs writer
// @namespace http://code.google.com/p/google-doc-navigation-panel
// @include https://docs.google.com/Doc*
// @include http://docs.google.com/Doc*
// @include https://docs.google.com/Edit*
// @include http://docs.google.com/Edit*
// ==/UserScript==
// vim:set shiftwidth=4 tabstop=4 expandtab

(function() {
    var get_heads = function() {
        var linevs = document.querySelectorAll('.kix-lineview');
        //var head_sizes = ['84px', '62px', '49px', '41px', '38px', '33px'];
        var heads = [];
        for (var i = 0; i < linevs.length; i +=1) {
            var linev = linevs[i];
            //var level = head_sizes.indexOf(linev.style.height) + 1;
            var level = parseInt(linev.style.height);
            //if (level > 35) {
            if ((level > 35) && (linev.innerText.length > 1) && (linev.innerText.length < 54) && (s = linev.querySelector('div > div > span > span')) && (s.style.fontWeight == 'bold')) {
                (function() {
                    var head = document.createElement('a');
                    head.href = 'javascript: void(0)';
                    head.style.display = 'block';
                    head.style.margin = '3px';
                    //head.style.fontSize = 10 - level + 'pt';
                    //head.style.paddingLeft = level - 1 + 'em';
                    head.style.fontSize = parseInt(level / 6) + 'px';
                    head.style.paddingLeft = 300 / level - 4 + 'em';
                    head.style.textDecoration = 'none';
                    head.style.color = '#000000';
                    head.innerText = linev.innerText.replace(/\n/g, '');
                    var p = linev.firstChild;
                    head.onclick = function() {
                        p.scrollIntoView(1);
                        void(0);
                    };
                    heads.push(head);
                }) ();
            }
        }
        return heads;
    }

    var last_heads = [];

    var update_nav = function() {
        if (!document.querySelector('#docs-editor-container')) {
            return;
        }
        heads = get_heads();
        if (heads == last_heads) {
            return
        }
        last_heads = heads;
        var editor_container = document.querySelector('#docs-editor-container');
        var editor =  document.querySelector('#docs-editor');
        var nav = document.querySelector('#docs-nav');
        if (!nav){
            var nav = document.createElement('div');
            nav.id = 'docs-nav';
            document.styleSheets[0].insertRule('div {box-sizing: border-box;}', 0);
            editor_container.style.cssText += 'position: relative; width: 100%;';
            editor.style.cssText = 'position: absolute; width: 100%; height: 100%; left: 0%';
            nav.style.cssText = 'position: absolute; left: 0; padding: 10px; width: 100%; overflow-y: auto; overflow-x: hidden;';
            editor_container.appendChild(nav);
        }
        if(nav.firstChild) {
            nav.removeChild(nav.firstChild);
        }
        navc = document.createElement('span');
        get_heads().forEach(function(i){navc.appendChild(i)});
        nav.appendChild(navc);
    }

    setInterval(update_nav, 4000);

    window.addEventListener('resize',function() {
        var p = document.querySelector('.kix-paginateddocumentplugin');
        var nav = document.querySelector('#docs-nav');
        nav.style.width = p.style.left;
    }, false);

})();

